from __future__ import absolute_import
import logging
from forest.api import Command, get_tool


#from pkgr.pkgr import Pkgr
from forest.resources.packages import PackageInstallList

logger = logging.getLogger('commands.pkgr.erase')

class Erase(Command):
    __register__ =  'commands.pkgr.Erase'

    def run(self, config):


        logger.debug('Erasing packages...')

        package_actions = PackageInstallList(config)
        with get_tool('system.chroot')(config.get_install_root()):
            for packageaction in package_actions:
                if not packageaction.requires_erase():
                    continue

                logger.info(packageaction)
                logger.debug('Erasing package {}'.format(packageaction.get_name()))

                get_tool('system.execute')(['rpm', '-e', '--nodeps', packageaction.get_name()])

